package com.yugao.fintech.draper.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yugao.fintech.draper.admin.convert.ClientDetailsConvert;
import com.yugao.fintech.draper.admin.entity.OauthClientDetails;
import com.yugao.fintech.draper.admin.mapper.OauthClientDetailsMapper;
import com.yugao.fintech.draper.admin.param.ClientDetailsQuery;
import com.yugao.fintech.draper.admin.service.Oauth2ClientDetailsService;
import com.yugao.fintech.draper.auth.dto.ClientDetailsDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class Oauth2ClientDetailsServiceImpl implements Oauth2ClientDetailsService {
    @Autowired
    private OauthClientDetailsMapper oauthClientDetailsMapper;

    @Override
    public IPage<ClientDetailsDTO> pageQuery(ClientDetailsQuery clientDetailsQuery) {
        Page<OauthClientDetails> pageInfo = oauthClientDetailsMapper.selectPage(Page.of(clientDetailsQuery.getPageNum(), clientDetailsQuery.getPageSize()),
                Wrappers.<OauthClientDetails>lambdaQuery().like(StrUtil.isNotBlank(clientDetailsQuery.getClientId()), OauthClientDetails::getClientId, clientDetailsQuery.getClientId()));
        return ClientDetailsConvert.INSTANCE.convertPage(pageInfo);
    }

    @Override
    public Boolean addClientDetails(ClientDetailsDTO clientDetailsDTO) {
        return oauthClientDetailsMapper.insert(ClientDetailsConvert.INSTANCE.convert(clientDetailsDTO)) > 0;
    }

    @Override
    public Boolean updateClientDetails(ClientDetailsDTO clientDetailsDTO) {
        return oauthClientDetailsMapper.updateById(ClientDetailsConvert.INSTANCE.convert(clientDetailsDTO)) > 0;
    }

    /**
     * 获取单条
     */
    @Override
    public ClientDetailsDTO findById(String id) {
        OauthClientDetails aDo = oauthClientDetailsMapper.selectById(id);
        return ClientDetailsConvert.INSTANCE.convert(aDo);
    }

    /**
     * 获取全部
     */
    @Override
    public List<ClientDetailsDTO> findAll() {
        return ClientDetailsConvert.INSTANCE.convertList(oauthClientDetailsMapper.selectList(Wrappers.emptyWrapper()));
    }

    /**
     * 删除
     */
    @Override
    public Boolean deleteClientDetails(String id) {
        return oauthClientDetailsMapper.deleteById(id) > 0;
    }

}